package Comp355Final.main;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class GUIConfig extends Activity{
	private Config myconfig;
	private Utility myutility;
	private String fontcolor;
	private String fontsize;
	private String charset;
	private SharedPreferences mysettings;
	private static int spinner_selected1=0,spinner_selected2=0,spinner_selected3=0;
    private TextView create_txtView(String name)
    {
         TextView tmp_txtview = new TextView(this);
         tmp_txtview.setText(Html.fromHtml(name));
         tmp_txtview.setTextColor(Color.BLACK);
         return tmp_txtview;
     }
     private LinearLayout create_linearlayout(TextView in_view)
     {
     	LinearLayout tmp_layout = new LinearLayout(this);
     	tmp_layout.addView(in_view);
     	return tmp_layout;
     } 
   //  private 
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView scllview = new ScrollView(this);
        ScrollView.LayoutParams scllview_param = new ScrollView.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        scllview.setBackgroundColor(Color.WHITE);
        myconfig = new Config(this);
        mysettings = myconfig.getSharedPreferences();
        loadConfigAndReturn();
        
        myutility = new Utility();
        scllview.setBackgroundDrawable(myconfig.return_bg());
        LinearLayout Llay = new LinearLayout(this);
        LinearLayout.LayoutParams Llay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        LinearLayout.LayoutParams tmpLay_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.WRAP_CONTENT);
        ViewGroup.LayoutParams viewGroup_param = new ViewGroup.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.WRAP_CONTENT);
        Llay.setOrientation(LinearLayout.VERTICAL);
        Llay.addView(this.create_linearlayout(this.create_txtView("<B><U>Default font color</U></B>")));
        Spinner color_spinner = new Spinner(this);
        String[] allcolor =Utility.returnAllColor();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Utility.returnAllColor()); 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        color_spinner.setAdapter(adapter);
        for(int i=0;i<allcolor.length;i++)
        {
        	if(allcolor[i].equals(this.fontcolor))
        	{
        		color_spinner.setSelection(i);
        	}
        }
        //color_spinner.setSelection(2);
        color_spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ 
                  public void onItemSelected(AdapterView adapterView, View view, int position, long id){ 
                	  if(spinner_selected1!=0)
                	  {
                	  GUIConfig.this.fontcolor=adapterView.getSelectedItem().toString();
                	  Utility.quick_dialog("You have selected "+GUIConfig.this.fontcolor,  GUIConfig.this);
                	  }
                	  else
                	  {
                		  spinner_selected1=1;
                	  }
                  } 
                  public void onNothingSelected(AdapterView arg0) { 
                  } 
        }); 
        Llay.addView(color_spinner,viewGroup_param);
        
        Llay.addView(this.create_linearlayout(this.create_txtView("<B><U>Default font size</U></B>")));
        Spinner font_spinner = new Spinner(this);
        float[] txtsize=Utility.returnAllTxtSize();
        String[] txtsize_str = new String[txtsize.length];
        for(int i=0;i<txtsize.length;i++)
        {
        	txtsize_str[i]=String.valueOf(txtsize[i]);
        	txtsize_str[i]= txtsize_str[i]+"px";
        }
        ArrayAdapter<String> font_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,txtsize_str); 
        font_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        font_spinner.setAdapter(font_adapter);
        
        for(int i=0;i<txtsize.length;i++)
        {
        	if(txtsize_str[i].replaceFirst("px", "").equals(this.fontsize))
        	{
        		font_spinner.setSelection(i);
        	}
        }
        font_spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ 
                  public void onItemSelected(AdapterView adapterView, View view, int position, long id){ 
                	  if(spinner_selected2!=0)
                	  {
                	  GUIConfig.this.fontsize=adapterView.getSelectedItem().toString();
                	  Utility.quick_dialog("You have selected "+GUIConfig.this.fontsize,  GUIConfig.this);
                	  }
                	  else
                	  {
                		  spinner_selected2=1;
                	  }
                  } 
                  public void onNothingSelected(AdapterView arg0) { 
                  } 
        }); 
        Llay.addView(font_spinner,viewGroup_param);
        
        Llay.addView(this.create_linearlayout(this.create_txtView("<B><U>Default charset</U></B>")));
        
        ArrayList<String> all_charset = myutility.return_charset_code();
        String[] charset_array = new String[all_charset.size()];
        for(int i=0;i<all_charset.size();i++)
        {
        	charset_array[i]=all_charset.get(i);
        }
       Spinner charset_spinner = new Spinner(this);
        ArrayAdapter<String> charset_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,charset_array); 
        charset_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        charset_spinner.setAdapter(charset_adapter);
        for(int i=0;i<charset_array.length;i++)
        {
        //	Utility.quick_dialog(charset_array[i]+charset, this);
        	if(charset_array[i].equals(this.charset))
        		charset_spinner.setSelection(i);
        }
        
        //charset_spinner.setSelection(2);
        charset_spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ 
                  public void onItemSelected(AdapterView adapterView, View view, int position, long id){ 
                	  if(spinner_selected3!=0)
                	  {
                		  GUIConfig.this.charset=adapterView.getSelectedItem().toString();
                		  Utility.quick_dialog("You have selected "+GUIConfig.this.charset,  GUIConfig.this);
                	  }
                	  else
                	  {
                		  spinner_selected3=1;
                	  }
                  } 
                  public void onNothingSelected(AdapterView arg0) { 
                  } 
        }); 
        Llay.addView(charset_spinner,viewGroup_param);
        LinearLayout tmp_layout = new LinearLayout(this);
        tmp_layout.setOrientation(LinearLayout.HORIZONTAL);
       
        Button okbutton = new Button(this);
        okbutton.setText("Submit");
        okbutton.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				saveConfigAndReturn();
				GUIConfig.this.finish();
			}
        
        });
        tmp_layout.addView(okbutton);
       
        Button cbutton = new Button(this);
        cbutton.setText("Cancel");
        cbutton.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				GUIConfig.this.finish();
			}
        
        });
        tmp_layout.addView(cbutton);
        Llay.addView(tmp_layout,tmpLay_param);
       // Utility.quick_dialog(color_now, this);
        
        scllview.addView(Llay, Llay_param);
        this.setTitle("Configuration");
        this.setContentView(scllview, scllview_param);
    }
    
    private void loadConfigAndReturn()
    {
    	fontcolor = myconfig.getActualfontColor();
    	fontsize = myconfig.getActualfontSize();
    	charset=myconfig.getActualcharset();
    	//Utility.quick_dialog(charset, this);
    }
    private void saveConfigAndReturn()
    {
    	
    	SharedPreferences.Editor editor = myconfig.getTheeditor();
    	editor.putString("DefaultfontColor", this.fontcolor);
    	//Utility.quick_dialog(fontsize, this);
    	fontsize=fontsize.replaceFirst("px", "");
    	editor.putString("DefaultfontSize", this.fontsize);
    	editor.putString("Defaultcharset", this.charset);
    	Utility.quick_dialog("Configuration saved.", this);
    	editor.commit();
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 0, 0, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert);
    	return super.onCreateOptionsMenu(menu); 
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId()==0)
    	{
    		Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
    		this.startActivity(int_ent);
    		finish();
    	}
    	return super.onOptionsItemSelected(item); 
    }
    
    @Override
    public void onResume()
    {
    	spinner_selected1=0;spinner_selected2=0;spinner_selected3=0;
    	super.onResume();
    }
}
